package com.project.weimi.utils;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 跟网络相关的工具类
 * 
 * @author Administrator
 *
 */
public class NetUtils {
	private Context mContext;
	private ConnectivityManager connectivityManager;
	private NetworkInfo networkInfo;

	public NetUtils(Context context) {
		this.mContext = context;
		connectivityManager = (ConnectivityManager) this.mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		networkInfo = connectivityManager.getActiveNetworkInfo();
	}

	public boolean isConnected() {
		return networkInfo != null && networkInfo.isConnected();
	}

	/**
	 * @return 0: 无网络 1:WIFI 2:其他（流量）
	 */
	public int getNetType() {
		if (!isConnected()) {
			return 0;
		}
		int type = networkInfo.getType();
		if (type == ConnectivityManager.TYPE_WIFI) {
			return 1;
		} else {
			return 2;
		}
	}

	private static Dialog dialog;

	/**
	 * 判断网络是否连接
	 *
	 * @param context
	 * @return
	 */
	public static boolean isConnected(Context context) {

		ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (null != connectivity) {

			NetworkInfo info = connectivity.getActiveNetworkInfo();
			if (null != info && info.isConnected()) {
				if (info.getState() == NetworkInfo.State.CONNECTED) {
					return true;
				}
			}
		}
		return false;
	}

	/**
	 * 判断是否是wifi连接
	 */
	public static boolean isWifi(Context context) {
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (cm == null)
			return false;
		return cm.getActiveNetworkInfo().getType() == ConnectivityManager.TYPE_WIFI;

	}

	/**
	 * 打开网络设置界面
	 */
	public static void openSetting(Activity activity) {
		Intent intent = new Intent("/");
		ComponentName cm = new ComponentName("com.android.settings", "com.android.settings.WirelessSettings");
		intent.setComponent(cm);
		intent.setAction("android.intent.action.VIEW");
		activity.startActivityForResult(intent, 0);
	}

	public static boolean checkNetWork(Context context) {
		if (context != null) {
			// 判断网络是否可用
			boolean isAvailable = netWorkIsAvailable(context);
			if (isAvailable) { // 如果不可用，弹出对话框
				showSettingWIFIDialog(context);
				return false;
			}
			return true;
		} else {
			return true;
		}
	}

	public static boolean netWorkIsAvailable(Context context) {
		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
		if (activeNetInfo != null) {
			if (activeNetInfo.isAvailable()) {
				return false;
			} else {
				return true;
			}
		}
		return true;
	}

	/**
	 * 无网络Dialog
	 * 
	 * @param context
	 */
	public static void showSettingWIFIDialog(final Context context) {
		if (dialog != null && dialog.isShowing()) {
			return;
		}
		final AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle("没有可用的网络");
		builder.setMessage("请开启4G或WIFI网络连接");
		dialog = builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				Intent intent = new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS);
				context.startActivity(intent);
			}
		}).setNeutralButton("取消", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {
				dialog.cancel();
			}
		}).create();
		dialog.show();
	}

}
